//
//  LXHistoryViewController.m
//  cat
//
//  Created by imera on 16/3/30.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXHistoryViewController.h"
#import "LXNavBarView.h"
#import "LXHistoryTableViewCell.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
#import "LXVideo.h"
#import "MoviePlayerViewController.h"

@interface LXHistoryViewController () <LXNavBarViewDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *historyVideoArr;

@property (nonatomic, weak) UITableView *tableView;

@end

@implementation LXHistoryViewController

- (NSMutableArray *)historyVideoArr
{
    if (_historyVideoArr == nil) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *file = [path stringByAppendingPathComponent:@"history.plist"];
        _historyVideoArr = [NSMutableArray arrayWithContentsOfFile:file];
        
        if (_historyVideoArr == nil) {
            _historyVideoArr = [NSMutableArray array];
        }
    }
    return _historyVideoArr;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initSubViews];
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    /** 导航条 */
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.leftTitle = @"历史记录";
    navBarView.rightTitle = @"清除记录";
    navBarView.myDelegate = self;
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    [self.view addSubview:navBarView];

    if (self.historyVideoArr.count == 0) { // 没有历史记录
        UILabel *remindLabel = [[UILabel alloc]init];
        remindLabel.text = @"暂无历史记录";
        remindLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:remindLabel];
        
        CGFloat remindLabelW = 150;
        CGFloat remindLabelH = 30;
        CGFloat remindLabelX = (self.view.frame.size.width - remindLabelW) / 2;
        CGFloat remindLabelY = (self.view.frame.size.height - remindLabelH) / 2;
        remindLabel.frame = CGRectMake(remindLabelX, remindLabelY, remindLabelW, remindLabelH);
        
        navBarView.rightBtn.enabled = NO;
    }else{
        UITableView *tableView = [[UITableView alloc]init];
        tableView.frame = CGRectMake(0, 20 + LXNavBarHeight, self.view.frame.size.width, self.view.frame.size.height - 20 - LXNavBarHeight);
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.delegate = self;
        tableView.dataSource = self;
            self.tableView = tableView;
        [self.view addSubview:tableView];
    }
    
}

#pragma mark - 自定义导航栏代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBtnClicked:(LXNavBarView *)navBarView
{
    //清除历史记录
    [self.historyVideoArr removeAllObjects];
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *file = [path stringByAppendingPathComponent:@"history.plist"];
    [self.historyVideoArr writeToFile:file atomically:YES];
    
    [self.tableView reloadData];
    
    navBarView.rightBtn.enabled = NO;
}

#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.historyVideoArr.count;
}

- (LXHistoryTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    LXHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[LXHistoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.dict = self.historyVideoArr[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *url = @"http://cat666.com/cat666-interface/index.php/index/getVideoInfo";
    NSDictionary *dict = self.historyVideoArr[indexPath.row];
    NSDictionary *params =@{@"videoid":dict[@"videoId"]};
    
    // 2.发送请求
    [mgr POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        LXVideo *video = [LXVideo initWithDict:dict[@"video"]];
        
        MoviePlayerViewController *moviePlayer = [[MoviePlayerViewController alloc]init];
        moviePlayer.video = video;
        [self presentViewController:moviePlayer animated:YES completion:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"您的网络开小差了..."];
    }];
}
@end
